﻿Imports GBDZafiro

Public Class FrmComprobante

    Private rgvDcomprobante As New DataGridViewRow()
    Private dtDcomp As DataTable

    Private Sub FrmComprobante_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'MyBase.bnNuevo_Click(sender, e)
        cargarTipoComp()
        cargarMcomp(GBDZafiro.MComprobante.ConsPrimero())

        AddHandler cmbTipo.SelectedIndexChanged, AddressOf cmbTipo_SelectedIndexChanged
        AddHandler dtpFecha.ValueChanged, AddressOf dtpFecha_ValueChanged
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As Object, e As System.EventArgs)
        'MyBase.bnNuevo_Click(sender, e)
        tbCompNo.Enabled = False
        tbEstado.Enabled = False
        cmbTipo.Enabled = True
        dtpFecha.Enabled = True
        gvDcomprobante.Enabled = True
    End Sub

    Private Sub btprimero_Click(sender As System.Object, e As System.EventArgs) Handles btprimero.Click
        cargarMcomp(GBDZafiro.MComprobante.ConsPrimero())
    End Sub

    Private Sub btUltimo_Click(sender As System.Object, e As System.EventArgs) Handles btUltimo.Click
        cargarMcomp(GBDZafiro.MComprobante.ConsUltimo())
    End Sub

    Private Sub btAdelante_Click(sender As System.Object, e As System.EventArgs) Handles btAdelante.Click
        Dim MComp As New GBDZafiro.MComprobante
        MComp.COD_CPB = tbCompNo.Text
        Dim dtMComp As DataTable = GBDZafiro.MComprobante.ConsSiguiente(MComp)
        If dtMComp IsNot Nothing Then
            cargarMcomp(dtMComp)
        End If
    End Sub

    Private Sub btAtras_Click(sender As System.Object, e As System.EventArgs) Handles btAtras.Click
        Dim MComp As New GBDZafiro.MComprobante
        MComp.COD_CPB = tbCompNo.Text
        Dim dtMComp As DataTable = GBDZafiro.MComprobante.ConsAnterior(MComp)
        If dtMComp IsNot Nothing Then
            cargarMcomp(dtMComp)
        End If
    End Sub

    Private Sub gvDcomprobante_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvDcomprobante.CellDoubleClick

        Dim i As Integer = e.ColumnIndex
        rgvDcomprobante = gvDcomprobante.CurrentRow
        abrirForms(i)
    End Sub

    Private Sub gvDcomprobante_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles gvDcomprobante.KeyDown
        If e.KeyCode = Keys.F3 Then
            rgvDcomprobante = gvDcomprobante.CurrentRow
            abrirForms(gvDcomprobante.CurrentCell.ColumnIndex)
        End If
    End Sub

    Private Sub cmbTipo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)
        fch_Tipochanged()
    End Sub

    Private Sub dtpFecha_ValueChanged(sender As System.Object, e As System.EventArgs)
        fch_Tipochanged()
    End Sub

    Private Sub gvDcomprobante_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs)
        rgvDcomprobante = gvDcomprobante.CurrentRow
        Dim i As Integer = e.ColumnIndex

        If i = 6 Then
            If Not rgvDcomprobante.Cells.Item("DB").Value = 0D Then
                removeEventos()
                rgvDcomprobante.Cells.Item("DB").Value = 0D
                addEventos()
            End If

        ElseIf i = 5 Then
            If Not rgvDcomprobante.Cells.Item("CR").Value = 0D Then
                removeEventos()
                rgvDcomprobante.Cells.Item("CR").Value = 0D
                addEventos()
            End If

        ElseIf i = 1 Then
            If rgvDcomprobante.Cells.Item("TERCERO").Value IsNot Nothing Then
                If Not rgvDcomprobante.Cells.Item("TERCERO").Value = "S" Then
                    rgvDcomprobante.Cells.Item("NIT").Value = ""
                End If
            End If

        ElseIf i = 4 Then
            If rgvDcomprobante.Cells.Item("CCOSTO").Value IsNot Nothing Then
                If Not rgvDcomprobante.Cells.Item("CCOSTO").Value = "S" Then
                    rgvDcomprobante.Cells.Item("COD_CENTRO").Value = ""
                End If
            End If

        End If

    End Sub

    Private Sub gvDcomprobante_CurrentCellChanged(sender As System.Object, e As System.EventArgs)
        Dim i As Integer = gvDcomprobante.Rows.Count

        rgvDcomprobante = gvDcomprobante.CurrentRow
        If rgvDcomprobante IsNot Nothing Then
            If rgvDcomprobante.Index = i - 1 Then
                If rgvDcomprobante.Cells.Item("COD_CUENTA").Value IsNot Nothing Then
                    gvDcomprobante.Rows.Add()
                End If
            End If
        End If

    End Sub

    Private Sub addEventos()
        AddHandler gvDcomprobante.CurrentCellChanged, AddressOf gvDcomprobante_CurrentCellChanged 'Agrego el evento de cambio de foco'
        AddHandler gvDcomprobante.CellValueChanged, AddressOf gvDcomprobante_CellValueChanged 'Agrego el evento de cambia el valor de una celda'

    End Sub

    Private Sub removeEventos()
        RemoveHandler gvDcomprobante.CurrentCellChanged, AddressOf gvDcomprobante_CurrentCellChanged 'Elimino el evento de cambio de foco'
        RemoveHandler gvDcomprobante.CellValueChanged, AddressOf gvDcomprobante_CellValueChanged 'Elimino el evento de cambia el valor de una celda'

    End Sub

    Private Sub fch_Tipochanged()
        If Not cmbTipo.Text.Length < 1 And Not tbCompNo.TextLength < 1 Then
            Dim mComp As New GBDZafiro.MComprobante
            mComp.FECHA_CPB = dtpFecha.Value
            'mComp.FECHA_CPB = "20140102"
            mComp.COD_TIPO_CPB = cmbTipo.SelectedValue
            mComp.COD_CPB = tbCompNo.Text

            initMcomp()
            removeEventos()
            gvDcomprobante.Rows.Clear()

            cargarMcomp(GBDZafiro.MComprobante.ConsxCpbTCpbFecha(mComp))
        End If
    End Sub

    Private Sub initMcomp()
        lbValorDeb.Text = ""
        lbValorCred.Text = ""
        lbValorDif.Text = ""
    End Sub

    Private Function filaVacia() As Boolean
        Dim rgvDcomprobante As New DataGridViewRow()
        rgvDcomprobante = gvDcomprobante.CurrentRow

        If rgvDcomprobante.Cells.Item("COD_CUENTA").Value Is Nothing Then 'ToString.Length < 1 Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub abrirForms(ByRef i As Integer)
        'Dim rGvDcomprobante As New DataGridViewRow()
        rgvDcomprobante = gvDcomprobante.CurrentRow

        Select Case i
            Case 0
                abrirFormBCuenta()

            Case 4
                abrirFormBCentro()

            Case 1
                abrirFormBTercero()

        End Select
    End Sub

    Private Sub abrirFormBCuenta()
        If filaVacia() Then
            Dim frmBCuenta As New FrmBCuenta
            Dim rgvDcomprobante As New DataGridViewRow()
            rgvDcomprobante = gvDcomprobante.CurrentRow

            If frmBCuenta.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                rgvDcomprobante.Cells.Item("COD_CUENTA").Value = frmBCuenta.Cuenta.COD_CUENTA
                rgvDcomprobante.Cells.Item("TERCERO").Value = frmBCuenta.Cuenta.TERCERO
                rgvDcomprobante.Cells.Item("CCOSTO").Value = frmBCuenta.Cuenta.CCOSTO

                If Not frmBCuenta.Cuenta.CCOSTO = "S" Then
                    rgvDcomprobante.Cells.Item("COD_CENTRO").Value = ""
                End If
                If Not frmBCuenta.Cuenta.TERCERO = "S" Then
                    rgvDcomprobante.Cells.Item("NIT").Value = ""
                End If

                rgvDcomprobante.Cells.Item("CR").Value = 0
                rgvDcomprobante.Cells.Item("DB").Value = 0
                rgvDcomprobante.Cells.Item("BASE").Value = 0

            End If
        End If
    End Sub

    Private Sub abrirFormBCentro()
        If Not rgvDcomprobante.Cells.Item("COD_CUENTA").Value.ToString.Length < 1 Then
            If rgvDcomprobante.Cells.Item("CCOSTO").Value IsNot Nothing Then
                If rgvDcomprobante.Cells.Item("CCOSTO").Value = "S" Then
                    Dim frmBCentro As New FrmBCentro

                    If frmBCentro.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                        rgvDcomprobante = gvDcomprobante.CurrentRow
                        rgvDcomprobante.Cells.Item("COD_CENTRO").Value = ""
                        rgvDcomprobante.Cells.Item("COD_CENTRO").Value = frmBCentro.ctrCosto.COD_CENTRO
                    End If

                End If
            End If
        End If
    End Sub

    Private Sub abrirFormBTercero()
        If Not rgvDcomprobante.Cells.Item("COD_CUENTA").Value.ToString.Length < 1 Then
            If rgvDcomprobante.Cells.Item("TERCERO").Value IsNot Nothing Then
                If rgvDcomprobante.Cells.Item("TERCERO").Value = "S" Then
                    Dim frmBTercero As New FrmBTercero
                    If frmBTercero.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                        rgvDcomprobante = gvDcomprobante.CurrentRow
                        rgvDcomprobante.Cells.Item("NIT").Value = ""
                        rgvDcomprobante.Cells.Item("NIT").Value = frmBTercero.Tercero.NIT
                    End If

                End If
            End If
        End If
    End Sub

    Private Sub cargarTipoComp()
        Dim dtTComp As DataTable = GBDZafiro.TipoComprobante.ConsultarAll()
        cmbTipo.DataSource = Nothing
        cmbTipo.DataSource = dtTComp
        cmbTipo.DisplayMember = "DESCRIPCION"
        cmbTipo.ValueMember = "COD_TIPO_CPB"
    End Sub

    Private Sub cargarMcomp(ByRef dtMComp As DataTable)
        'Dim dtMComp As DataTable = GBDZafiro.MComprobante.getFirsMcomp()
        For Each rComp As DataRow In dtMComp.Rows
            tbCompNo.Text = rComp("COD_CPB")
            lbValorDeb.Text = "$ " + Format(CType(rComp("DEBITO").ToString, Decimal), "#,##0.00")
            lbValorCred.Text = "$ " + Format(CType(rComp("CREDITO").ToString, Decimal), "#,##0.00")
            cmbTipo.SelectedValue = rComp("COD_TIPO_CPB")

            lbValorDif.Text = "$ " + Format(CType((rComp("DEBITO") - rComp("CREDITO")).ToString, Decimal), "#,##0.00")
            dtpFecha.Value = rComp("FECHA_CPB")

            If Not DBNull.Value.Equals(rComp("ESTADO")) Then
                If rComp("ESTADO") = "A" Then
                    tbEstado.Text = "ACTIVO"
                Else
                    tbEstado.Text = "INACTIVO"
                End If
            End If
            cargarGvDcomp(rComp("ID_CPB"))
        Next

        If dtMComp Is Nothing Then
            gvDcomprobante.Rows.Add()
        End If
    End Sub

    Private Sub cargarGvDcomp(idComp As Decimal)
        removeEventos()

        Dim rGv As New DataGridViewRow()
        Dim dtTercero As DataTable
        Dim tercero As New GBDZafiro.Tercero
        Dim Dcomp As New GBDZafiro.DComprobante
        Dcomp.ID_CPB = idComp

        gvDcomprobante.Rows.Clear()
        dtDcomp = GBDZafiro.DComprobante.ConsxIdCPB(Dcomp)
        If dtDcomp.Rows.Count < 1 Then
            Exit Sub
        End If

        With dtDcomp
            .Columns.Add("NIT")
        End With


        For Each rDcomp As DataRow In dtDcomp.Rows
            If Not DBNull.Value.Equals(rDcomp("ID_TERCERO")) Then
                tercero.ID_TERCERO = rDcomp("ID_TERCERO")
                dtTercero = GBDZafiro.Tercero.ConsxIdTercero(tercero)
                For Each rTercero As DataRow In dtTercero.Rows
                    rDcomp("NIT") = rTercero("NIT")
                Next
            End If
        Next

        If gvDcomprobante.Columns.Count = 0 Then
            gvDcomprobante.Columns.Add("COD_CUENTA", "COD_CUENTA")
            gvDcomprobante.Columns.Add("NIT", "NIT")
            gvDcomprobante.Columns.Add("NUM_SOPORTE", "NUM_SOPORTE")
            gvDcomprobante.Columns.Add("CONCEPTO", "CONCEPTO")
            gvDcomprobante.Columns.Add("COD_CENTRO", "COD_CENTRO")
            gvDcomprobante.Columns.Add("DB", "DB")
            gvDcomprobante.Columns.Add("CR", "CR")
            gvDcomprobante.Columns.Add("BASE", "BASE")
            gvDcomprobante.Columns.Add("TERCERO", "TERCERO")
            gvDcomprobante.Columns.Add("CCOSTO", "CCOSTO")
            gvDcomprobante.Columns("TERCERO").Visible = False
            gvDcomprobante.Columns("CCOSTO").Visible = False
        End If

        For Each rDcomp As DataRow In dtDcomp.Rows
            gvDcomprobante.Rows.Add(rDcomp("COD_CUENTA"), rDcomp("NIT"), rDcomp("NUM_SOPORTE"), rDcomp("CONCEPTO"), _
                                    rDcomp("COD_CENTRO"), rDcomp("DB"), rDcomp("CR"), rDcomp("BASE"))
            'addCuenta(rDcomp("COD_CUENTA").ToString)
        Next
        addEventos()

        With gvDcomprobante
            .Columns("COD_CUENTA").DisplayIndex = 0
            .Columns("NIT").DisplayIndex = 1
            .Columns("NUM_SOPORTE").DisplayIndex = 2
            .Columns("CONCEPTO").DisplayIndex = 3
            .Columns("COD_CENTRO").DisplayIndex = 7

            .Columns("COD_CUENTA").HeaderText = "CUENTA"
            .Columns("DB").HeaderText = "DEBITO"
            .Columns("CR").HeaderText = "CREDITO"
            .Columns("COD_CENTRO").HeaderText = "CENTRO"
            .Columns("NUM_SOPORTE").HeaderText = "DOC"

            .Columns("DB").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns("CR").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .Columns("BASE").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

            .Rows.Add()
        End With

    End Sub

End Class
